package io.renren.modules.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.modules.manage.constant.PayStyleEnum;
import io.renren.modules.manage.constant.StateEnum;
import io.renren.modules.manage.entity.FreshUserEntity;
import io.renren.modules.manage.entity.ProductCategoryEntity;
import io.renren.modules.manage.service.FreshUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.manage.dao.OrderDao;
import io.renren.modules.manage.entity.OrderEntity;
import io.renren.modules.manage.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
    @Autowired
    private FreshUserService freshUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = params.get("key").toString();
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        if (key.length() > 0) {
            queryWrapper.like("order_id", key);
        }
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        page.getRecords().forEach(orderEntity -> {
            String userId = orderEntity.getUserId();
            FreshUserEntity user = freshUserService.getById(userId);
            String stateName = StateEnum.getEnumByCode(orderEntity.getState()).getMsg();
            String payStyleName = PayStyleEnum.getEnumByCode(orderEntity.getPayStyle()).getMsg();
            orderEntity.setUserName(user.getUsername());
            orderEntity.setStateName(stateName);
            orderEntity.setPayStyleName(payStyleName);
        });

        return new PageUtils(page);
    }

    @Override
    public void updateState(String id) {
        UpdateWrapper<OrderEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_id", id);
        updateWrapper.set("state",StateEnum.ARRIVE.getCode());
        this.update(updateWrapper);
    }

}
